//
//  CollectionController.m
//  News
//
//  Created by xalo on 16/3/9.
//  Copyright © 2016年 News. All rights reserved.
//

#import "CollectionController.h"
#import "FMDatabase+Database.h"
#import "NewsModel.h"
#import "NewsDetailViewController.h"
#import "VideoDetailViewController.h"
#import "ABPableViewController.h"
#import "CollectVideoViewCell.h"

#import "DetialModel.h"
#import "PicDetialViewController.h"

#import <UIImageView+WebCache.h>
#import "VideoModel.h"


#import "CollectVideoViewCell.h"
@interface CollectionController ()

@property (nonatomic,strong) NSArray *sections;
@property (nonatomic,strong) NSMutableArray *cmsArray;
@property (nonatomic,strong) NSMutableArray *videoArray;

@property (nonatomic,strong) NSMutableArray *detialArray;

@end

@implementation CollectionController


- (NSMutableArray *)cmsArray {
    if (!_cmsArray) {
        _cmsArray = [NSMutableArray array];
    }
    return _cmsArray;
}

- (NSMutableArray *)videoArray {
    if (!_videoArray) {
        _videoArray = [NSMutableArray array];
    }
    return _videoArray;
}

-(NSMutableArray *)detialArray {
    
    if (!_detialArray) {
        
        _detialArray = [NSMutableArray array];
    }
    return _detialArray;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
    self.sections = @[@"我的新闻",@"我的图片",@"我的视频"];
    self.title = @"我的收藏";
    //
    
    
    
    NSArray *array1 = [[FMDatabase shareManager] selectCollectionByCategory:@"cms"];
    for (NewsModel *model in array1) {
        
        [self.cmsArray addObject:model];
    }
    

    
    NSArray *array2 = [[FMDatabase shareManager] selectCollectionByCategory:@"video"];
    for (VideoModel *model in array2) {
        [self.videoArray addObject:model];
    }
    
    
    NSArray *array3 = [[FMDatabase shareManager] selectCollectionByCategory:@"Model"];
    [self.detialArray addObject:array3];
    

    [self.tableView registerNib:[UINib nibWithNibName:@"CollectVideoViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sections.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return self.sections[section];

}

//返回单元格个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return self.cmsArray.count;
        
    }
    
    if (section == 2) {

         return self.videoArray.count;
        
    }

    if (section == 1) {
        
        
        return  [[FMDatabase shareManager] selectOtherCollection].count;
        
    }
    
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    
    static NSString *identifer = @"cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifer];
    }
    
    
    // 新闻
    if (indexPath.section == 0) {
        
        NewsModel *model = self.cmsArray[indexPath.row];
        cell.textLabel.text = model.title;
        return cell;
    }
    
    if (indexPath.section == 1) {
        
        NSArray *array = [[FMDatabase shareManager] selectOtherCollection][indexPath.row];
        
        if (array.count >= 1) {
            
            NSInteger count = array.count -1;
            NSDictionary *dic = array[count];
            
            cell.textLabel.text = dic[@"link"];
            
           return cell;
        }
   
}
    
    
    // 视频
    if (indexPath.section == 2) {
        
//        CollectVideoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        
        VideoModel *model = self.videoArray[indexPath.row];
//        cell.titleLabel.text = model.title;
//        [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.kpic]];
        cell.textLabel.text = model.title;
//        cell.detailTextLabel.text = @"03-16 14:20";
        
        return cell;
    }

   
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.section == 0) {   //新闻区
        
        NewsModel *model = self.cmsArray[indexPath.row];
        
        NewsDetailViewController *newsDetailVC = [[UIStoryboard storyboardWithName:@"NewsDetailViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"detailVC"];
        newsDetailVC.newsModel = model;
        [self presentViewController:newsDetailVC animated:NO completion:nil];
    }
    
    if (indexPath.section == 1) {   // 图片区
        
        NSArray *array = [[FMDatabase shareManager] selectOtherCollection][indexPath.row];
        
        PicDetialViewController *piConreoll =[[UIStoryboard storyboardWithName:@"Picture" bundle:nil] instantiateViewControllerWithIdentifier:@"PicDetial"];
        
            NSInteger count = array.count -1;
            
              NSDictionary *dic = array[count];
            
            piConreoll.comments  = dic[@"comments"];
            piConreoll.link =dic[@"link"];
            
            [self presentViewController:piConreoll animated:YES completion:nil];
        
    }
    
    if (indexPath.section == 2) {   // 视频区

        
        VideoModel *model = self.videoArray[indexPath.row];
        VideoDetailViewController *detailVC = [[UIStoryboard storyboardWithName:@"Video" bundle:nil] instantiateViewControllerWithIdentifier:@"detail"];
        detailVC.videoModel = model;
        [self presentViewController:detailVC animated:YES completion:nil];
        

    

    }
    
}


        
        















// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//     Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        if (indexPath.section == 0) {   // 新闻区
            
            NewsModel *model = self.cmsArray[indexPath.row];
            [[FMDatabase shareManager] removeCollectionModelByID:model.ID];
            [self.cmsArray removeObject:model];
            
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        
        if (indexPath.section == 1) {    // 图片区
            

            NSArray *array = [[FMDatabase shareManager] selectOtherCollection][indexPath.row];
            
            NSInteger count = array.count -1;
            
            NSDictionary *dic = array[count];
            
            [[FMDatabase shareManager] removeCollectionModelByID:dic[@"comments"]];
            
            [self.detialArray removeObject:array];
            
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
            
        }
        
        if (indexPath.section == 2) {   // 视频区
            
            
            VideoModel *model = self.videoArray[indexPath.row];
            [[FMDatabase shareManager] removeCollectionModelByID:model.ID];
            
            [self.videoArray removeObject:model];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
            
            
        }
        
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
